<template>
  <div>
    <!-- header -->
    <market-header></market-header>
    <!-- main -->
    <div class="container">
      <div class="header">&nbsp;&nbsp;我的购物车</div>
      <hr>
      <cart-box v-for="(item,i) in goodsList"
                :key="item._id"
                :info='item'
                @del="delM(i)"
                @check="check"></cart-box>
      <div class="clear">
        <p class="price">合计:￥{{ price }}</p>
        <span class="btn"
              @click="clear">去结算({{ num }})</span>
      </div>
    </div>
    <!-- footer -->
    <market-footer></market-footer>
  </div>
</template>

<script>
import MarketHeader from "@/components/common/MarketHeader"
import MarketFooter from "@/components/common/MarketFooter"
import CartBox from '@/components/common/cartBox'
export default {
  components: {
    MarketHeader,
    MarketFooter,
    CartBox
  },
  data () {
    return {
      goodsList: [],
      switchList: [],
    }
  },
  computed: {
    price () {
      return this.$store.state.price;
    },
    num () {
      return this.$store.state.num;
    }
  },
  created () {
    this.$http.post('http://localhost:3000/users/findCart', { username: localStorage.getItem('username') }).then(response => {
      this.goodsList = response.data.data
    })
  },
  methods: {
    delM (i) {
      this.goodsList.splice(i, 1);
    },
    check ([obj, flag]) {
      if (flag == 'on') {
        this.switchList.push(obj);
      } else {
        this.switchList.forEach((item, index) => {
          if (item.goodsId == obj.goodsId) {
            if (flag == 'off') {
              this.switchList.splice(index, 1);
            } else {
              this.switchList.splice(index, 1, obj);
            }
          }
        })
      }
    },
    clear () {

    }
  }
}
</script>

<style lang="less" scoped>
.container {
  width: 1060px;
  min-height: 500px;
  margin: 100px auto;
  position: relative;

  .header {
    color: #f10;
    font-weight: bold;
    padding-bottom: 5px;
  }
  .clear {
    position: absolute;
    right: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 50px;
    width: 300px;
    box-sizing: border-box;
    padding: 0 10px;
    border: 1px solid #ccc;
    background-color: #fff;

    .btn {
      padding: 5px 10px;
      border-radius: 20px;
      background-color: #f30;
      color: #fff;
      font-size: 14px;
      cursor: pointer;
    }
  }
}
</style>